package com.redisson.controller;


import com.redisson.dto.ApiResponseDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequestMapping("/user")
@RestController
public class RedisClusterController {

    @Autowired
    private RedisTemplate redisTemplate;

    private static final String KEY = "user:id:";

    @RequestMapping("/get")
    public ApiResponseDto<String> get(String id) {
        String value = (String) redisTemplate.opsForValue().get(KEY + id);
        if(StringUtils.isEmpty(value)){
            redisTemplate.opsForValue().set(KEY + id, "hello world");
            value = (String) redisTemplate.opsForValue().get(KEY + id);
        }
        return ApiResponseDto.success(value);
    }

}
